import { defaultStrat, strats } from "./config"

export const _toString = Object.prototype.toString

export function isObject(obj) {
  return _toString.call(obj) === '[object Object]'
}

export function isFunction(...funcs) {
  return funcs.every(func => typeof func === "function")
}

export const hasOwnProperty = function (obj, property) {
  return Object.prototype.hasOwnProperty.call(obj, property)
}

export function mergeOptions(parent, child, vm) {

  let options = {}

  for (let key in parent) {
    mergeField(key)
  }

  for (let key in child) {
    if (!hasOwnProperty(parent, key)) {
      // 如果父节点没有就会走到这里
      mergeField(key)
    }
  }

  if (child.mixins) {
    child.mixins.forEach(mixin => {
      parent = mergeOptions(parent, mixin)
    })
  }

  function mergeField(field) {
    let strat = strats[field] || defaultStrat
    options[field] = strat(parent[field], child[field], vm, field)
  }

  return options


}